//
//  ModifyUserViewController.m
//  Final
//
//  Created by student on 2021/12/17.
//  Copyright © 2021 Lenrony. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "ModifyUserViewController.h"

@interface ModifyUserViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate, UITextFieldDelegate>{
    
    float screen_width;
    float screen_height;
}

@property(strong, nonatomic) User* user;
@property(strong, nonatomic) ModelController* mc;

@property(strong, nonatomic) UIImagePickerController* imgPicker;
@property(strong, nonatomic) UIScrollView* scv;
@property(strong, nonatomic) UIImageView* headImgView;
@property(strong, nonatomic) UIGestureRecognizer* selectGesture;
@property(strong, nonatomic) UILabel* uidTitleLabel;
@property(strong, nonatomic) UILabel* nameTitleLabel;
@property(strong, nonatomic) UILabel* passwordTitleLabel;
@property(strong, nonatomic) UILabel* passwordVerifyTitleLabel;
@property(strong, nonatomic) UILabel* emailTitleLabel;
@property(strong, nonatomic) UILabel* phoneTitleLabel;
@property(strong, nonatomic) UILabel* hobbyTitleLabel;
@property(strong, nonatomic) UILabel* contentTitleLabel;
@property(strong, nonatomic) UILabel* uidLabel;
@property(strong, nonatomic) UITextField* nameLabel;
@property(strong, nonatomic) UITextField* passwordLabel;
@property(strong, nonatomic) UITextField* passwordVerifyLabel;
@property(strong, nonatomic) UITextField* emailLabel;
@property(strong, nonatomic) UITextField* phoneLabel;
@property(strong, nonatomic) UITextView* hobbyLabel;
@property(strong, nonatomic) UITextView* contentLabel;
@property(strong, nonatomic) UILabel* nameCheckLabel;
@property(strong, nonatomic) UILabel* passwordCheckLabel;
@property(strong, nonatomic) UILabel* passwordVerifyCheckLabel;
@property(strong, nonatomic) UIButton* modifyBtn;
@property(strong, nonatomic) UIAlertController* alert;
@property(nonatomic) BOOL isNameValid;
@property(nonatomic) BOOL isPasswordValid;
@property(nonatomic) BOOL isPasswordVerifyValid;
@property(nonatomic) BOOL isFinished;

@end

@implementation ModifyUserViewController

- (ModifyUserViewController *)initWithUser: (User *)user
{
    self = [super init];
    self.user = user;
    self.mc = [ModelController getInstance];
    return self;
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    screen_width = [UIScreen mainScreen].bounds.size.width;
    screen_height = [UIScreen mainScreen].bounds.size.height;
    self.isNameValid = true;
    self.isPasswordValid = true;
    self.isPasswordVerifyValid = true;
    self.isFinished = false;
    
    self.scv = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, screen_width, screen_height)];
    [self.scv addSubview:self.uidLabel];
    [self.scv addSubview:self.nameLabel];
    [self.scv addSubview:self.passwordLabel];
    [self.scv addSubview:self.passwordVerifyLabel];
    [self.scv addSubview:self.emailLabel];
    [self.scv addSubview:self.phoneLabel];
    [self.scv addSubview:self.hobbyLabel];
    [self.scv addSubview:self.contentLabel];
    [self.scv addSubview:self.headImgView];
    [self.scv addSubview:self.uidTitleLabel];
    [self.scv addSubview:self.nameTitleLabel];
    [self.scv addSubview:self.passwordTitleLabel];
    [self.scv addSubview:self.passwordVerifyTitleLabel];
    [self.scv addSubview:self.emailTitleLabel];
    [self.scv addSubview:self.phoneTitleLabel];
    [self.scv addSubview:self.hobbyTitleLabel];
    [self.scv addSubview:self.contentTitleLabel];
    [self.scv addSubview:self.nameCheckLabel];
    [self.scv addSubview:self.passwordCheckLabel];
    [self.scv addSubview:self.passwordVerifyCheckLabel];
    [self.scv addSubview:self.modifyBtn];
    float y = self.modifyBtn.frame.origin.y + self.modifyBtn.bounds.size.height + 100;
    self.scv.contentSize = CGSizeMake(screen_width, y);
    [self.view addSubview:self.scv];
    
    self.imgPicker = [[UIImagePickerController alloc]init];
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}

-(void)modifyClicked
{
    if(!(self.isNameValid && self.isPasswordValid && self.isPasswordVerifyValid)){
        [self showAlertWithTitle:@"修改失败" andText:@"表单中有表项不合法"];
        return;
    }
    User* user = [[User alloc]initWithID:self.user.u_id andName:self.nameLabel.text andPassword:self.passwordLabel.text andHead:self.headImgView.image andEmail:self.emailLabel.text andphone:self.phoneLabel.text andHobby:self.hobbyLabel.text andContent:self.contentLabel.text];
    self.mc.cur_user = user;
    int ret = [self.mc modifyUser:self.mc.cur_user];//必须用self.mc.cur_user而不是user，否则析构，发不出去
    if(ret == 0){
        self.isFinished = true;
        [self showAlertWithTitle:@"修改成功" andText:@""];
    }
    else{
        [self showAlertWithTitle:@"修改失败" andText:@"网络连接异常"];
    }
}

- (void)showAlertWithTitle: (NSString*)title
                   andText: (NSString*)text
{
    self.alert = [UIAlertController alertControllerWithTitle:title message:text preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:self.alert animated:YES completion:nil];
    [self performSelector:@selector(hideAlert) withObject:self.alert afterDelay:1.5];
}

- (void)hideAlert
{
    [self.alert dismissViewControllerAnimated:YES completion:^{
        if(self.isFinished){
            //[self.navigationController.parentViewController viewWillAppear:YES];
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
}

//选择图片
-(IBAction) selectImg
{
    [self presentViewController:self.imgPicker animated:YES completion:nil];
}

//成功后返回
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
    UIImage* img = [info objectForKey:UIImagePickerControllerOriginalImage];
    self.headImgView.image = img;
}

- (void)nameTextFieldDidChange:(id)sender {
    self.isNameValid = (self.nameLabel.text.length <= 30);
    if(self.isNameValid){
        self.nameCheckLabel.text = @"✅用户名合法";
        self.nameCheckLabel.textColor = [UIColor greenColor];
    }
    else{
        self.nameCheckLabel.text = @"❌用户名长度30以内";
        self.nameCheckLabel.textColor = [UIColor redColor];
    }
}

- (void)passwordTextFieldDidChange:(id)sender {
    NSString *passwordRegex = @"^[a-zA-Z0-9]{8,20}+$";
    NSPredicate *passwordTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", passwordRegex];
    self.isPasswordValid = [passwordTest evaluateWithObject:self.passwordLabel.text];
    if(self.isPasswordValid){
        self.passwordCheckLabel.text = @"✅密码合法";
        self.passwordCheckLabel.textColor = [UIColor greenColor];
    }
    else{
        self.passwordCheckLabel.text = @"❌密码由数字字母组成，长度为8-20";
        self.passwordCheckLabel.textColor = [UIColor redColor];
    }
}

- (void)passwordVerifyFieldDidChange:(id)sender {
    self.isPasswordVerifyValid = [self.passwordLabel.text isEqualToString:self.passwordVerifyLabel.text];
    if(self.isPasswordVerifyValid){
        self.passwordVerifyCheckLabel.text = @"✅密码合法";
        self.passwordVerifyCheckLabel.textColor = [UIColor greenColor];
    }
    else{
        self.passwordVerifyCheckLabel.text = @"❌两次输入密码不一致";
        self.passwordVerifyCheckLabel.textColor = [UIColor redColor];
    }
}

//LazyLoad
-(UIImageView*) headImgView
{
    if(_headImgView == nil){
        _headImgView = [[UIImageView alloc] initWithFrame:CGRectMake(screen_width / 2 - 60, 60, 120, 120)];
        _headImgView.layer.cornerRadius = 10;
        _headImgView.layer.masksToBounds = YES;
        _headImgView.image = self.user.head_img;
        [_headImgView addGestureRecognizer:self.selectGesture];
        _headImgView.userInteractionEnabled = YES;
    }
    return _headImgView;
}

-(UIGestureRecognizer*) selectGesture
{
    if(_selectGesture == nil){
        _selectGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(selectImg)];
    }
    return _selectGesture;
}

-(UILabel*) uidTitleLabel
{
    if(_uidTitleLabel == nil){
        _uidTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 220, 80, 30)];
        _uidTitleLabel.text = @"学号：";
        _uidTitleLabel.textAlignment = NSTextAlignmentRight;
        [_uidTitleLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _uidTitleLabel;
}

-(UILabel*) nameTitleLabel
{
    if(_nameTitleLabel == nil){
        _nameTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 260, 80, 30)];
        _nameTitleLabel.text = @"用户名：";
        _nameTitleLabel.textAlignment = NSTextAlignmentRight;
        [_nameTitleLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _nameTitleLabel;
}

-(UILabel*) passwordTitleLabel
{
    if(_passwordTitleLabel == nil){
        _passwordTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 340, 80, 30)];
        _passwordTitleLabel.text = @"密码：";
        _passwordTitleLabel.textAlignment = NSTextAlignmentRight;
        [_passwordTitleLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _passwordTitleLabel;
}

-(UILabel*) passwordVerifyTitleLabel
{
    if(_passwordVerifyTitleLabel == nil){
        _passwordVerifyTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 420, 80, 30)];
        _passwordVerifyTitleLabel.text = @"确认密码：";
        _passwordVerifyTitleLabel.textAlignment = NSTextAlignmentRight;
        [_passwordVerifyTitleLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _passwordVerifyTitleLabel;
}

-(UILabel*) emailTitleLabel
{
    if(_emailTitleLabel == nil){
        _emailTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 500, 80, 30)];
        _emailTitleLabel.text = @"邮箱：";
        _emailTitleLabel.textAlignment = NSTextAlignmentRight;
        [_emailTitleLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _emailTitleLabel;
}

-(UILabel*) phoneTitleLabel
{
    if(_phoneTitleLabel == nil){
        _phoneTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 540, 80, 30)];
        _phoneTitleLabel.text = @"电话：";
        _phoneTitleLabel.textAlignment = NSTextAlignmentRight;
        [_phoneTitleLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _phoneTitleLabel;
}

-(UILabel*) hobbyTitleLabel
{
    if(_hobbyTitleLabel == nil){
        _hobbyTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 580, 80, 30)];
        _hobbyTitleLabel.text = @"爱好：";
        _hobbyTitleLabel.textAlignment = NSTextAlignmentRight;
        [_hobbyTitleLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _hobbyTitleLabel;
}

-(UILabel*) contentTitleLabel
{
    if(_contentTitleLabel == nil){
        _contentTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 790, 80, 30)];
        _contentTitleLabel.text = @"简介：";
        _contentTitleLabel.textAlignment = NSTextAlignmentRight;
        [_contentTitleLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _contentTitleLabel;
}

-(UILabel*) uidLabel
{
    if(_uidLabel == nil){
        _uidLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 220, screen_width - 100, 30)];
        _uidLabel.text = self.user.u_id;
        _uidLabel.textAlignment = NSTextAlignmentLeft;
        [_uidLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _uidLabel;
}

-(UITextField*) nameLabel
{
    if(_nameLabel == nil){
        _nameLabel = [[UITextField alloc]initWithFrame:CGRectMake(90, 260, screen_width - 100, 30)];
        _nameLabel.text = self.user.name;
        _nameLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _nameLabel.layer.cornerRadius = 3;
        _nameLabel.layer.borderWidth = 1;
        _nameLabel.textAlignment = NSTextAlignmentLeft;
        [_nameLabel setFont:[UIFont systemFontOfSize:15]];
        _nameLabel.delegate = self;
        [_nameLabel addTarget:self action:@selector(nameTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _nameLabel;
}

-(UILabel*) nameCheckLabel
{
    if(_nameCheckLabel == nil){
        _nameCheckLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 300, screen_width - 100, 30)];
        _nameCheckLabel.textColor = [UIColor greenColor];
        _nameCheckLabel.textAlignment = NSTextAlignmentLeft;
        [_nameCheckLabel setFont:[UIFont systemFontOfSize:15]];
        _nameCheckLabel.text = @"✅用户名合法";
    }
    return _nameCheckLabel;
}

-(UITextField*) passwordLabel
{
    if(_passwordLabel == nil){
        _passwordLabel = [[UITextField alloc]initWithFrame:CGRectMake(90, 340, screen_width - 100, 30)];
        _passwordLabel.text = self.user.password;
        _passwordLabel.secureTextEntry = YES;
        _passwordLabel.autocorrectionType = UITextAutocorrectionTypeNo;
        _passwordLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _passwordLabel.layer.cornerRadius = 3;
        _passwordLabel.layer.borderWidth = 1;
        _passwordLabel.textAlignment = NSTextAlignmentLeft;
        [_passwordLabel setFont:[UIFont systemFontOfSize:15]];
        _passwordLabel.delegate = self;
        [_passwordLabel addTarget:self action:@selector(passwordTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [_passwordLabel addTarget:self action:@selector(passwordVerifyFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _passwordLabel;
}

-(UILabel*) passwordCheckLabel
{
    if(_passwordCheckLabel == nil){
        _passwordCheckLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 380, screen_width - 100, 30)];
        _passwordCheckLabel.textColor = [UIColor greenColor];
        _passwordCheckLabel.text = @"✅密码合法";
        _passwordCheckLabel.textAlignment = NSTextAlignmentLeft;
        [_passwordCheckLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _passwordCheckLabel;
}

-(UITextField*) passwordVerifyLabel
{
    if(_passwordVerifyLabel == nil){
        _passwordVerifyLabel = [[UITextField alloc]initWithFrame:CGRectMake(90, 420, screen_width - 100, 30)];
        _passwordVerifyLabel.text = self.user.password;
        _passwordVerifyLabel.secureTextEntry = YES;
        _passwordVerifyLabel.autocorrectionType = UITextAutocorrectionTypeNo;
        _passwordVerifyLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _passwordVerifyLabel.layer.cornerRadius = 3;
        _passwordVerifyLabel.layer.borderWidth = 1;
        _passwordVerifyLabel.textAlignment = NSTextAlignmentLeft;
        [_passwordVerifyLabel setFont:[UIFont systemFontOfSize:15]];
        _passwordVerifyLabel.delegate = self;
        [_passwordVerifyLabel addTarget:self action:@selector(passwordVerifyFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
        [_passwordVerifyLabel addTarget:self action:@selector(passwordTextFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    }
    return _passwordVerifyLabel;
}

-(UILabel*) passwordVerifyCheckLabel
{
    if(_passwordVerifyCheckLabel == nil){
        _passwordVerifyCheckLabel = [[UILabel alloc]initWithFrame:CGRectMake(90, 460, screen_width - 100, 30)];
        _passwordVerifyCheckLabel.textColor = [UIColor greenColor];
        _passwordVerifyCheckLabel.textAlignment = NSTextAlignmentLeft;
        [_passwordVerifyCheckLabel setFont:[UIFont systemFontOfSize:15]];
        _passwordVerifyCheckLabel.text = @"✅密码合法";
    }
    return _passwordVerifyCheckLabel;
}

-(UITextField*) emailLabel
{
    if(_emailLabel == nil){
        _emailLabel = [[UITextField alloc]initWithFrame:CGRectMake(90, 500, screen_width - 100, 30)];
        _emailLabel.text = self.user.email;
        _emailLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _emailLabel.layer.cornerRadius = 3;
        _emailLabel.layer.borderWidth = 1;
        _emailLabel.textAlignment = NSTextAlignmentLeft;
        [_emailLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _emailLabel;
}

-(UITextField*) phoneLabel
{
    if(_phoneLabel == nil){
        _phoneLabel = [[UITextField alloc]initWithFrame:CGRectMake(90, 540, screen_width - 100, 30)];
        _phoneLabel.text = self.user.phone;
        _phoneLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _phoneLabel.layer.cornerRadius = 3;
        _phoneLabel.layer.borderWidth = 1;
        _phoneLabel.textAlignment = NSTextAlignmentLeft;
        [_phoneLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _phoneLabel;
}

-(UITextView*) hobbyLabel
{
    if(_hobbyLabel == nil){
        _hobbyLabel = [[UITextView alloc]initWithFrame:CGRectMake(90, 580, screen_width - 100, 200)];
        _hobbyLabel.text = self.user.hobby;
        _hobbyLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _hobbyLabel.layer.cornerRadius = 3;
        _hobbyLabel.layer.borderWidth = 1;
        _hobbyLabel.textAlignment = NSTextAlignmentLeft;
        [_hobbyLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _hobbyLabel;
}

-(UITextView*) contentLabel
{
    if(_contentLabel == nil){
        _contentLabel = [[UITextView alloc]initWithFrame:CGRectMake(90, 790, screen_width - 100, 200)];
        _contentLabel.text = self.user.content;
        _contentLabel.layer.borderColor = [UIColor lightGrayColor].CGColor;
        _contentLabel.layer.cornerRadius = 3;
        _contentLabel.layer.borderWidth = 1;
        _contentLabel.textAlignment = NSTextAlignmentLeft;
        [_contentLabel setFont:[UIFont systemFontOfSize:15]];
    }
    return _contentLabel;
}

-(UIButton*) modifyBtn
{
    if(_modifyBtn == nil){
        _modifyBtn = [[UIButton alloc]initWithFrame:CGRectMake(screen_width / 2 - 60, 1030, 120, 30)];
        [_modifyBtn setTitle:@"确认修改" forState:UIControlStateNormal];
        _modifyBtn.backgroundColor = [UIColor colorWithRed:0 green:0.7f blue:0 alpha:1];
        _modifyBtn.layer.cornerRadius = 5;
        [_modifyBtn addTarget:self action:@selector(modifyClicked) forControlEvents:UIControlEventTouchUpInside];
    }
    return _modifyBtn;
}

@end
